package com.demo.swingquickticket.swing.view.ticket;

import com.alibaba.fastjson.JSONObject;
import com.demo.swingquickticket.entity.Order;
import com.demo.swingquickticket.entity.SeatTypeContents;
import com.demo.swingquickticket.entity.User;
import com.demo.swingquickticket.service.DataService;
import com.demo.swingquickticket.service.GrabTicketService;
import com.demo.swingquickticket.swing.layoutUtil.WindowUtil;
import com.demo.swingquickticket.util.SpringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class TicketConfirmDialog extends JDialog {

    private List<String> seatType = new ArrayList<>();

    private List<String> stationCode;

    private String data;

    private String from;

    private String to;

    private GrabTicketService grabTicketService = SpringUtils.getBean("grabTicketService");

    public TicketConfirmDialog(Frame frame, List<String> stationCode, String data, String from, String to) {
        super(frame);
        this.stationCode = stationCode;
        this.data = data;
        this.from = from;
        this.to = to;
        init(stationCode);
    }

    private void init(List<String> stationCode) {
        this.setTitle("购票确认");
        this.setSize(WindowUtil.getWindowWidth() / 2, WindowUtil.getWindowHeight() / 4);
        this.setLayout(new BorderLayout(5,5));

        JLabel stationLabel = new JLabel("选中车次" + JSONObject.toJSONString(stationCode));

        JCheckBox jCheckBox1 = new JCheckBox("硬座");
        jCheckBox1.addItemListener(e -> {
            if (jCheckBox1.isSelected()) {
                seatType.add(SeatTypeContents.硬座.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.硬座.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox2 = new JCheckBox("软座");
        jCheckBox2.addItemListener(e -> {
            if (jCheckBox2.isSelected()) {
                seatType.add(SeatTypeContents.软座.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.软座.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox3 = new JCheckBox("硬卧");
        jCheckBox3.addItemListener(e -> {
            if (jCheckBox3.isSelected()) {
                seatType.add(SeatTypeContents.硬卧.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.硬卧.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox4 = new JCheckBox("软卧");
        jCheckBox4.addItemListener(e -> {
            if (jCheckBox4.isSelected()) {
                seatType.add(SeatTypeContents.软卧.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.软卧.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox5 = new JCheckBox("高级软卧");
        jCheckBox5.addItemListener(e -> {
            if (jCheckBox5.isSelected()) {
                seatType.add(SeatTypeContents.高级软卧.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.高级软卧.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox6 = new JCheckBox("商务座_特等座");
        jCheckBox6.addItemListener(e -> {
            if (jCheckBox6.isSelected()) {
                seatType.add(SeatTypeContents.商务座_特等座.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.商务座_特等座.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox7 = new JCheckBox("一等座");
        jCheckBox7.addItemListener(e -> {
            if (jCheckBox7.isSelected()) {
                seatType.add(SeatTypeContents.一等座.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.一等座.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox8 = new JCheckBox("二等座");
        jCheckBox8.addItemListener(e -> {
            if (jCheckBox8.isSelected()) {
                seatType.add(SeatTypeContents.二等座.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.二等座.getSeatTypeCode());
            }
        });
        JCheckBox jCheckBox9 = new JCheckBox("动卧");
        jCheckBox9.addItemListener(e -> {
            if (jCheckBox9.isSelected()) {
                seatType.add(SeatTypeContents.动卧.getSeatTypeCode());
            } else {
                seatType.remove(SeatTypeContents.动卧.getSeatTypeCode());
            }
        });

        JPanel userPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
        JLabel name = new JLabel("姓名");
        JLabel id = new JLabel("身份证");
        JLabel phone = new JLabel("手机号");
        JTextField nameField = new JTextField(5);
        JTextField idField = new JTextField(10);
        JTextField phoneField = new JTextField(10);

        userPanel.add(name);
        userPanel.add(nameField);
        userPanel.add(id);
        userPanel.add(idField);
        userPanel.add(phone);
        userPanel.add(phoneField);

        JPanel checkPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
        checkPanel.add(jCheckBox1);
        checkPanel.add(jCheckBox2);
        checkPanel.add(jCheckBox3);
        checkPanel.add(jCheckBox4);
        checkPanel.add(jCheckBox5);
        checkPanel.add(jCheckBox6);
        checkPanel.add(jCheckBox7);
        checkPanel.add(jCheckBox8);
        checkPanel.add(jCheckBox9);

        JPanel detailPanel = new JPanel(new BorderLayout(5,5));
        detailPanel.add(userPanel, BorderLayout.NORTH);
        detailPanel.add(checkPanel, BorderLayout.CENTER);

        JButton ok = new JButton("确定");

        ok.addActionListener(e -> {
            Order order = new Order();
            order.setUser(new User(nameField.getText(), phoneField.getText(), idField.getText()));
            order.setDate(this.data);
            order.setFrom(this.from);
            order.setTo(this.to);
            order.setStationTrainCode(this.stationCode);
            order.setSeatType(this.seatType);
            DataService.addWork(order);
            this.dispose();
        });

        this.add(stationLabel, BorderLayout.NORTH);

        this.add(detailPanel, BorderLayout.CENTER);

        this.add(ok, BorderLayout.SOUTH);
        this.setVisible(true);
    }
}
